home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / lib / idcache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-11  |  2.4 KB  |  99 lines

  1. /* idcache.c -- map user and group IDs, cached for speed
  2.    Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <pwd.h>
  21. #include <grp.h>
  22. #ifndef POSIX
  23. struct passwd *getpwuid ();
  24. struct group *getgrgid ();
  25. #endif
  26.  
  27. char *xmalloc ();
  28. char *xstrdup ();
  29.  
  30. struct userid
  31. {
  32.   unsigned short uid;
  33.   char *name;
  34.   struct userid *next;
  35. };
  36.  
  37. static struct userid *user_alist;
  38.  
  39. /* Translate UID to a login name, with cache.  */
  40.  
  41. char *
  42. getuser (uid)
  43.      unsigned short uid;
  44. {
  45.   register struct userid *tail;
  46.   struct passwd *pwent;
  47.   char usernum_string[20];
  48.  
  49.   for (tail = user_alist; tail; tail = tail->next)
  50.     if (tail->uid == uid)
  51.       return tail->name;
  52.  
  53.   pwent = getpwuid (uid);
  54.   tail = (struct userid *) xmalloc (sizeof (struct userid));
  55.   tail->uid = uid;
  56.   tail->next = user_alist;
  57.   if (pwent == 0)
  58.     {
  59.       sprintf (usernum_string, "%u", (unsigned short) uid);
  60.       tail->name = xstrdup (usernum_string);
  61.     }
  62.   else
  63.     tail->name = xstrdup (pwent->pw_name);
  64.   user_alist = tail;
  65.   return tail->name;
  66. }
  67.  
  68. /* Use the same struct as for userids.  */
  69. static struct userid *group_alist;
  70.  
  71. /* Translate GID to a group name, with cache.  */
  72.  
  73. char *
  74. getgroup (gid)
  75.      unsigned short gid;
  76. {
  77.   register struct userid *tail;
  78.   struct group *grent;
  79.   char groupnum_string[20];
  80.  
  81.   for (tail = group_alist; tail; tail = tail->next)
  82.     if (tail->uid == gid)
  83.       return tail->name;
  84.  
  85.   grent = getgrgid (gid);
  86.   tail = (struct userid *) xmalloc (sizeof (struct userid));
  87.   tail->uid = gid;
  88.   tail->next = group_alist;
  89.   if (grent == 0)
  90.     {
  91.       sprintf (groupnum_string, "%u", (unsigned int) gid);
  92.       tail->name = xstrdup (groupnum_string);
  93.     }
  94.   else
  95.     tail->name = xstrdup (grent->gr_name);
  96.   group_alist = tail;
  97.   return tail->name;
  98. }
  99.